<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Miembros extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('miembros_model');
		$this->load->model('videos_model');		
		$this->load->library('pagination');
		$this->load->library('tank_auth');
		$this->load->helper('url');
		$this->load->helper('form');		
		$this->load->helper('text');
	}
	
	public function index($page = FALSE)
	{
		$per_page = 8;
		
		$this->pagination->initialize(array(
			'base_url'=>site_url('miembros/index/'),
			'total_rows'=>count($this->videos_model->get_published()),
			'per_page'=>$per_page,
			'first_link'=>'&nbsp;Primera&nbsp;',
			'last_link'=>'&nbsp;Última&nbsp;'
		));
		
		$data['pages'] = $this->pagination->create_links();
		$data['title'] = 'Miembros - Starscamp';
		$data['result'] = $this->miembros_model->get_published();
		$data['videos'] = $this->_get_videos($page, $per_page);
		foreach($data['result'] as $row)
		{
			$row->logos = $this->miembros_model->get_logos($row->id);
		}
		
		$this->load->view('templates/header', $data);
		$this->load->view('miembros', $data);
		$this->load->view('templates/footer', $data);
	}
	
	private function _get_videos($offset = 0, $per_page = 10)
	{
		$result = array();
		$total_result = $this->videos_model->get_published();
		$num_items = count($total_result) - $offset;
		$num_items = $num_items > $per_page ? $per_page : $num_items;
		
		for($i = 0; $i < $num_items; $i++)
		{
			$result[] = $total_result[$i + $offset];
		}
		
		return $result;
	}
	
	public function videos($id = FALSE)
	{
		// validate id
		if(!$this->videos_model->get_id($id))
		{
			// if no session, cancel preview
			if(!$this->tank_auth->is_logged_in())
			{
				show_404();
			}
		}
		
		$data['title'] = 'Videos - Starscamp';
		$data['row'] = $this->videos_model->get_id($id);
		$this->load->view('templates/header', $data);
		$this->load->view('miembros-videos', $data);
		$this->load->view('templates/footer', $data);
	}
}

/* End of file miembros.php */
/* Location: ./application/controllers/miembros.php */